<template>
  <el-dialog
    class="dia-style"
    :title="title"
    :visible.sync="open"
    width="70%"
    append-to-body
    :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="菜单类型" prop="menuType">
            <el-radio-group v-model="form.menuType">
              <el-radio label="M">目录</el-radio>
              <el-radio label="C">菜单</el-radio>
              <el-radio label="F">按钮</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入菜单名称" maxlength="50"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上级菜单">
            <treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item label="菜单图标" prop="icon">
            <el-popover placement="bottom-start" width="460" trigger="click" @show="$refs['iconSelect'].reset()">
              <IconSelect ref="iconSelect" @selected="selected" />
              <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                <svg-icon
                  v-if="form.icon"
                  slot="prefix"
                  :icon-class="form.icon"
                  class="el-input__icon"
                  style="height: 32px; width: 16px" />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="form.orderNum" controls-position="right" :min="0" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.menuType == 'M'">
          <el-col :span="12">
            <el-form-item prop="appCode">
              <span slot="label">
                <el-tooltip content="外部应用时配置该项" placement="top">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                关联应用
              </span>
              <el-select v-model="form.appCode" placeholder="请选择" style="width: 100%">
                <el-option label="基础平台" value="" />
                <el-option v-for="item in appOptions" :key="item.appCode" :label="item.appName" :value="item.appCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="选择是外链则路由地址需要以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              是否外链
            </span>
            <el-radio-group v-model="form.isFrame">
              <el-radio :label="0">是</el-radio>
              <el-radio :label="1">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="选择不是外链则页面是嵌入iframe的页面" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              是否内嵌
            </span>
            <el-radio-group v-model="form.isInline">
              <el-radio v-for="item in isInlineOption" :label="item.value" :key="item.value">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F' && form.isInline == '0'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="内链访问以`http(s)://`开头" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              内嵌地址
            </span>
            <el-input v-model="form.pathUrl" placeholder="请输入内嵌地址" maxlength="124" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F' && form.isInline == '0'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="当前页面标识字段" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              内嵌字段
            </span>
            <el-input v-model="form.pathKey" placeholder="请输入内嵌字段标识" maxlength="124" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item prop="path">
            <span slot="label">
              <el-tooltip
                content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头"
                placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              路由地址
            </span>
            <el-input v-model="form.path" placeholder="请输入路由地址" maxlength="200"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType == 'C'">
          <el-form-item prop="component">
            <span slot="label">
              <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              组件路径
            </span>
            <el-input v-model="form.component" placeholder="请输入组件路径" maxlength="256"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'M'">
          <el-form-item>
            <el-input v-model="form.permission" placeholder="请输入权限标识" maxlength="100" />
            <span slot="label">
              <el-tooltip
                content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasPermi('system:user:list')`)"
                placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              权限字符
            </span>
        </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType == 'C'">
          <el-form-item>
            <el-input v-model="form.query" placeholder="请输入路由参数" maxlength="256" />
            <span slot="label">
              <el-tooltip content='访问路由的默认传递参数，如：`{"id": 1, "name": "ry"}`' placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              路由参数
            </span>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType == 'C'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="选择是则会被`keep-alive`缓存，需要匹配组件的`name`和地址保持一致" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              是否缓存
            </span>
            <el-radio-group v-model="form.isCache">
              <el-radio :label="0">缓存</el-radio>
              <el-radio :label="1">不缓存</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              显示状态
            </span>
            <el-radio-group v-model="form.visible">
              <el-radio v-for="dict in visibleStatus" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 'F'">
          <el-form-item>
            <span slot="label">
              <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
              菜单状态
            </span>
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in menusStatus" :key="dict.value" :label="dict.value">{{ dict.label }} </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { addMenu, updateMenu } from '@/api/system/menu'
import { listApp } from '@/api/system/app'
import IconSelect from '@/components/IconSelect'

export default {
  name: 'editDialog',
  components: { IconSelect, Treeselect },
  props: {
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
    menuOptions: {
      type: Array,
      default() {
        return []
      },
    },
    title: {
      type: String,
      default() {
        return '标题'
      },
    },
    visibleStatus: {
      type: Array,
      default() {
        return []
      },
    },
    menusStatus: {
      type: Array,
      default() {
        return []
      },
    },
  },
  watch: {
    visibleStatus: {
      handler(newVal) {},
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      form: {
        isInline: '1',
        isCache: '1',
        appCode: '',
      },
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        name: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
        orderNum: [{ required: true, message: '菜单顺序不能为空', trigger: 'blur' }],
        path: [{ required: true, message: '路由地址不能为空', trigger: 'blur' }],
      },
      isInlineOption: [
        { label: '是', value: '0' },
        { label: '否', value: '1' },
      ],
      appOptions: [],
    }
  },
  created() {
    listApp().then((res) => {
      this.appOptions = res
    })
  },
  watch: {
    formData: {
      handler(newVal) {
        this.form = newVal
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    show() {
      this.open = true
      this.reset()
    },
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMenu(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.$emit('submitForm')
            })
          } else {
            addMenu(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.$emit('submitForm')
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        pathUrl: undefined,
        pathKey: undefined,
        parentId: 0,
        menuName: undefined,
        icon: undefined,
        menuType: 'M',
        orderNum: undefined,
        isInline: '1',
        isFrame: '1',
        isCache: '0',
        visible: '0',
        status: '0',
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.el-row {
  display: flex;
  flex-wrap: wrap;
}
</style>
